/**
 * [一句话描述该类的功能]
 *
 * @author : [kai]
 * @version : [v1.0]
 * @createTime : [2022/10/25 13:59]
 */


package com.find.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.find.controller.utils.R;
import com.find.domain.Item;
import com.find.domain.User;
import com.find.service.ItemService;
import com.find.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@RestController
@Api(tags="失物相关")
@RequestMapping("/items")
public class ItemController {

    @Autowired
    private ItemService itemService;

    @Autowired
    private UserService userService;
    //查询所有item
    @ApiOperation("获取所有失物")
    @GetMapping
    public R getNotClaimed(HttpServletResponse response) {
        response.setStatus(200);
        return new R(true, itemService.notClaimed());
    }

    @GetMapping("/all")
    public R getAll(HttpServletResponse response) {
        response.setStatus(200);
        return new R(true, itemService.list());
    }

    //添加item
    @ApiOperation("添加失物")
    @PostMapping
    public R save(@RequestBody Item item, HttpServletRequest request, HttpServletResponse response) throws IOException {
        if(item.getUserId()==null){
            String name=request.getHeader("Cookie");
            User user=userService.getByName(name);
            if(user==null){
                return new R(false,"用户名错误");
            }
            int id=user.getId();
            item.setUserId(id);
        }
        boolean flag = itemService.saveItem(item);
        response.setStatus(flag?200:500);
        return new R(flag, flag ? "添加成功" : "添加失败!");
    }

    //更新item
    @ApiOperation("更新失物")
    @PutMapping
    public R update(@RequestBody Item item,HttpServletResponse response) throws IOException {
        boolean flag = itemService.updateItem(item);
        response.setStatus(flag?200:500);
        return new R(flag, flag ? "修改成功" : "修改失败!");
    }

    //根据id删除item
    @ApiOperation("删除失物")
    @DeleteMapping("{id}")
    public R delById(@PathVariable Integer id,HttpServletResponse response) {
        boolean flag=itemService.delById(id);
        response.setStatus(flag?200:500);
        return  new R(flag,flag?"删除成功":"删除失败");
    }

    //根据id查询
    @ApiOperation("查询失物")
    @GetMapping("/select")
    @ApiImplicitParams({
            @ApiImplicitParam(name="item_id",type ="",required = false,value=""),
            @ApiImplicitParam(name="item_name",type ="",required = false,value=""),
            @ApiImplicitParam(name="item_des",type ="",required = false,value="描述"),
            @ApiImplicitParam(name="user_id",type ="",required = false,value=""),
            @ApiImplicitParam(name="item_img_url",type ="",required = false,value=""),
            @ApiImplicitParam(name="item_pos",type ="",required = false,value=""),
            @ApiImplicitParam(name="item_claim",type ="",required = false,value="是否已经认领,0未认领，1认领，默认0"),
            @ApiImplicitParam(name="item_time",type ="",required = false,value=""),
    })
    public R getByMap(@RequestParam Map<String, Object> map,HttpServletResponse response) {
        System.out.println(map);
        response.setStatus(200);
        return new R(true, itemService.listByMap(map));
    }

    //分页查询
    @ApiOperation("分页获取")
    @GetMapping("{currentPage}/{pageSize}")
    public R getPage(@PathVariable int currentPage,@PathVariable int pageSize,Item item,HttpServletResponse response) {
        IPage<Item> page = itemService.getPage(currentPage, pageSize, item);
        if(currentPage > page.getPages()) {
            page = itemService.getPage((int) page.getPages(), pageSize, item);
        }
        response.setStatus(200);
        return new R(true, page);
    }


}
